package com.fms.kccc.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxStatus;
import com.androidquery.callback.BitmapAjaxCallback;
import com.fms.kccc.R;
import com.fms.kccc.library.ImageViewFullWidth;
import com.fms.kccc.object.ImageContent;
import com.fms.kccc.util.Common;

public class ContentViewPagerAdapter extends PagerAdapter {
	private static final int OFFSET_LOAD_MORE = 2;
	private List<ImageContent> data;
	private LayoutInflater mInflater;
	private boolean isUseImageBackup = false;
	private AQuery aq;
	private boolean memCache = true;
	private boolean fileCache = true;
	private OnLoadListener listener;
	private boolean isFirstPage;

	public ContentViewPagerAdapter(List<ImageContent> data, Context context) {
		super();
		this.data = data;
		this.mInflater = LayoutInflater.from(context);
		this.aq = new AQuery(context);
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public boolean isViewFromObject(View view, Object object) {
		return view == ((View) object);
	}

	@Override
	public Object instantiateItem(ViewGroup container, int position) {
		View view = mInflater.inflate(R.layout.lv_item_image, container, false);
		TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
		TextView tvSource = (TextView) view.findViewById(R.id.tv_source);
		ProgressBar pbLoading = (ProgressBar) view
				.findViewById(R.id.pb_loading);
		final ImageViewFullWidth ivImage = (ImageViewFullWidth) view
				.findViewById(R.id.iv_image);
		final ImageContent item = data.get(position);

		String imageURL = isUseImageBackup ? getURLBackup(item
				.getImageURLBackup()) : item.getSmallImageURL();
		Log.d("kccc", "get image from " + imageURL);
		aq.id(ivImage)
				.progress(pbLoading)
				.image(imageURL, memCache, fileCache,
						ivImage.getMeasuredHeight(), R.drawable.error,
						new BitmapAjaxCallback() {

							@Override
							public void callback(String url, ImageView iv,
									Bitmap bm, AjaxStatus status) {
								Log.d("kccc", "status " + status.getCode()
										+ "-" + status.getError());
								if (status.getCode() != 200) {
									Log.d("kccc", "load image null");
									String imageURL = isUseImageBackup ? item
											.getSmallImageURL()
											: getURLBackup(item
													.getImageURLBackup());
									Log.d("kccc", "change to load image from "
											+ imageURL);
									aq.id(ivImage).image(imageURL, memCache,
											fileCache,
											ivImage.getMeasuredHeight(),
											R.drawable.error);
								} else {
									iv.setImageBitmap(bm);
								}
							}

						});
		tvTitle.setText(item.getTitle());
		tvSource.setText(item.getSource());
		((ViewPager) container).addView(view);
		if (position == data.size() - OFFSET_LOAD_MORE) {
			if (listener != null) {
				listener.onLoadMore();
			}
		}
		return view;
	}

	@Override
	public void destroyItem(View collection, int position, Object view) {
		((ViewPager) collection).removeView((View) view);
	}

	public String getURLBackup(String url) {
		return String.format(Common.RequestURL.GET_BACKUP_IMAGE, url);
	}

	public void setOnLoadListener(OnLoadListener listener) {
		this.listener = listener;
	}

	public interface OnLoadListener {
		public void onLoadMore();
	}

	public void setFirstPage(boolean isFirstPage) {
		this.isFirstPage = isFirstPage;
	}

	@Override
	public int getItemPosition(Object object) {
		if (isFirstPage) {
			return POSITION_NONE;
		}
		return super.getItemPosition(object);
	}

	@Override
	public void notifyDataSetChanged() {
		super.notifyDataSetChanged();
	}
}
